package com.liuyk.commonlib.utility;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;

import com.liuyk.commonlib.App;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件常见操作工具
 * <p>
 * Created by liuyakui on 2020/2/4
 */
public final class FileUtils {

    //获取根目录
    public static File getParentDir() {
        File parentFile = new File(Environment.getExternalStorageDirectory(), DeviceUtils.getAppName(App.context));
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        return parentFile;
    }

    //获取图片根目录
    public static File getPictureDir() {
        File parentFile = new File(getParentDir(), "Picture");
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        return parentFile;
    }

    //获取文件根目录
    public static File getFileDir() {
        File parentFile = new File(getParentDir(), "File");
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        return parentFile;
    }

    //获取视频根目录
    public static File getVideoDir() {
        File parentFile = new File(getParentDir(), "Video");
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        return parentFile;
    }

    /**
     * 通知图库更新
     *
     * @param context   context
     * @param imageFile 图片文件
     * @throws Exception 如果发生异常，表示外部文件存在问题，这个异常应该就交由外部处理
     */
    private static void updateAlbum(Context context, File imageFile) {
        if (null == imageFile) {
            return;
        }
        String imagePath = imageFile.getAbsolutePath();
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + imagePath)));
    }

    public static File bitmapToFile(Context context, Bitmap bitmap) {
        File imgFile = new File(getPictureDir(), System.currentTimeMillis() + ".jpg");
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(imgFile));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (bos != null) {
                try {
                    bos.flush();
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        updateAlbum(context, imgFile);
        return imgFile;
    }

    /**
     * 将数据写入文件
     *
     * @param path    绝对路径
     * @param content 内容
     */
    public static void saveContentFile(String path, String content) {
        File conFile = new File(path);
        try {
            if (!conFile.exists()) {
                conFile.createNewFile();
            }
        } catch (Exception e) {
            return;
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(conFile, true);
            fos.write(content.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
